/***************************************************************************
 *   Copyleft 2006-2007 by												   *
 *	 Iwanicki Krzysztof													   *
 *   <gamma.mini.pw.edu.pl/~iwanickik>									   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/
#ifndef __MATRIX4D_H__
#define __MATRIX4D_H__

#include "../../Types/Types.h"
#include "./Point4D.h"
#include "../../Macros/Macros.h"

#define ROW_COUNT			4
#define COLUMN_COUNT		4

class Matrix4D: public Serializable {
public:
	union {	
		struct {
			cFloat mValues[16];			// matrix[row * column] == matrix[row][column]
		};
		struct {
			cFloat m11, m12, m13, m14;	// mValues[0]...mValues[3]
			cFloat m21, m22, m23, m24;	// mValues[4]...mValues[7]
			cFloat m31, m32, m33, m34;	// mValues[8]...mValues[11]
			cFloat m41, m42, m43, m44;	// mValues[12]...mValues[15]
		};
	};

	cFloat Determinant() const;
public:

    /**************************************************************
    * Constructors                                                *
    ***************************************************************/
	Matrix4D();
	Matrix4D(cFloat* tab);
	Matrix4D(const Matrix4D& matrix);
	~Matrix4D() { }
        
    /**************************************************************
    * Operators                                                   *
    ***************************************************************/
	Matrix4D operator*(const Matrix4D& b);
	Point4D operator*(const Point4D& b);
	Matrix4D operator*(const cFloat fFactor);
	Matrix4D& operator=(const Matrix4D& matrix);
	cFloat& operator()(cInt row, cInt col);

	
    /**************************************************************
    * Other helpful functions                                     *
    ***************************************************************/
	cVoid ToString();
	Matrix4D Invert();
	Matrix4D Transpose();
	static Matrix4D Translation(const Point4D& point4D);
	static Matrix4D RotationX(cFloat fAngle);
	static Matrix4D RotationY(cFloat fAngle);
	static Matrix4D RotationZ(cFloat fAngle);
	static Matrix4D Scaling(cFloat fFactor);
	static Matrix4D Scaling(cFloat fXFactor, cFloat fYFactor, cFloat fZFactor);
	static Matrix4D Perspective(cFloat fFov);
	static Matrix4D PerspectiveLeftEye(cFloat fFov, cFloat fEyeDistance);
	static Matrix4D PerspectiveRightEye(cFloat fFov, cFloat fEyeDistance);
	static Matrix4D Identity();
	static Matrix4D Zeros();

	virtual TiXmlElement* Serialize(TiXmlElement* parent);
};                                                                 

#endif // __MATRIX4D_H__